//---------------------------------------------------------------------
// File: StringFormatterMultipleStep.cs
// 
// Summary: 
//
// Copyright (c) Hammersmith & Fulham Bridge Partnership. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Xml;
using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

namespace BizUnit.Extensions
{
	/// <summary>
	/// The StringFormatter multiple step is used to format a base with multiple parameters
	/// and then to load the result into a specified key in the context
	/// </summary>
	/// 
	/// <remarks>
	/// The following shows an example of the Xml representation of this test step.
	/// 
	/// <code escaped="true">
	/// <TestStep assemblyPath="BizUnit.Extensions.dll" typeName="BizUnit.Extensions.StringFormatterMultipleStep">
	/// <BaseString>exec NotifyNewCRM '{0}','1'</BaseString>
	/// <ParamStringCollection>
	///		<ParamString takeFromCtx="InputPartyID"></ParamString>
	/// </ParamStringCollection>
	/// <ContextKey>OutputString</ContextKey>
	/// </TestStep>
	///	</code>
	///	
	///	<list type="table">
	///		<listheader>
	///			<term>Tag</term>
	///			<description>Description</description>
	///		</listheader>
	///		<item>
	///			<term>BaseString</term>
	///			<description>The base string with placeholders for the new parameters</description>
	///		</item>
	///		<item>
	///			<term>ParamStringCollection</term>
	///			<description>A collection of parameter strings</description>
	///		</item>
	///		<item>
	///			<term>ParamString</term>
	///			<description>Each parameter string to pick up and this parameter may already exist in the context</description>
	///		</item>
	///	</list>
	///	</remarks>

	public class StringFormatterMultipleStep: ITestStep
	{

		/// <summary>
		/// ITestStep.Execute() implementation
		/// </summary>
		/// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
		/// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
		public void Execute(XmlNode testConfig, Context context)
		{
			// read test config...
			string BaseString = context.ReadConfigAsString(testConfig,"BaseString");
			
			XmlNodeList paramlist = testConfig.SelectNodes("./ParamStringCollection/ParamString");
            XmlNode validationConfig = testConfig.SelectSingleNode("ValidationStep");
			context.LogInfo("Number of Nodes is {0}",paramlist.Count.ToString());
			string[] ParamArray = new string[paramlist.Count];
			
			for (int arrayindex = 0; arrayindex < paramlist.Count;arrayindex++)
			{
				string xpathstr = string.Format("./*[local-name()='ParamStringCollection']/*[local-name()='ParamString' and position() = {0}]",arrayindex + 1);
				context.LogInfo(xpathstr);
				context.LogInfo(testConfig.OuterXml);
				ParamArray[arrayindex] = context.ReadConfigAsString(testConfig,xpathstr);
			}
	
			string contextKey = context.ReadConfigAsString(testConfig,"ContextKey");
				
			string result = String.Format(BaseString,ParamArray);
		
			context.LogInfo(string.Format("String Formatter created : {0}", result));
			context.Add(contextKey,result,true);
            context.ExecuteValidator(null, validationConfig);
				
			
		}

	}
}
